package me.kevinwalker.core.base;

import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public interface BaseMongoDB<T> extends MongoCollection<Document> {

    void insert(T info);

    void insertList(List<T> t);

    long deleteById(Object id);

    long deleteByEntity(Object entity);

    List<T> query(Bson filters);

    T queryById(Object id);

    void createCollection(String name);

    @SuppressWarnings("unchecked")
    default Class<T> getEntityClass() {
        Type[] types = getClass().getGenericInterfaces();
        for (Type type : types) {
            if (type.getTypeName().startsWith(BaseMongoDB.class.getName())) {
                ParameterizedType pt = (ParameterizedType) type;
                return (Class<T>) pt.getActualTypeArguments()[0];
            }
        }
        return null;
    }
}
